<?php
/**
 * Paginator Ajax Component: Component build ajax pagging
 *
 * @author VuNN
 * @date 13/08/2012
 */
class PaginatorAjaxComponent extends Component {
	/**
	 * Build url pagging
     * @author VuNN
     * 
	 * @param $total
	 * @param $perPage,
	 * @param $numPageShow
	 * @param $classNormal
	 * @param $page_name
	 * @param $classCurrent
	 * @param $pageCurrent
	 *
	 * @return string pagging
	 */
	function build($total, $perPage, $numPageShow=10, $classNormal='',$page_name='page_no', $classCurrent = '',$pageCurrent = 0)
	{
		$content='';
		$totalpage = ceil($total/$perPage);
		if($pageCurrent == 0)
			$currentpage = 1;
		else
			$currentpage = $pageCurrent;
		// $currentpage=round($currentpage);
		if($classCurrent == '')
			$classCurrent = 'class="paging-active"';
		else
			$classCurrent = 'class="'.$classCurrent.'"';
		if($classNormal){
			$classText = $classNormal;
			$classNormal = 'class="'.$classNormal.'"';
		}
		if($currentpage <= 0 || $currentpage > $totalpage)
		{
			$currentpage = 1;
		}

		if($currentpage > ($numPageShow/2))
		{
			$startpage = $currentpage-floor($numPageShow/2);
			if(($totalpage - $startpage) < $numPageShow)
			{
				$startpage = $totalpage - $numPageShow + 1;
			}
		}
		else
		{
			$startpage = 1;
		}
		if($startpage < 1)
		{
			$startpage = 1;
		}
		//Link den trang truoc
		if($currentpage > 1)
		{
			$content.= '<li><A class="prev '.$classText.'" href="javascript:;" rel = "'.($currentpage-1).'" >Prev</A></li>';
		}
		//Danh sach cac trang
		if($startpage > 1)
		{
			$content.= '<li><a '.$classNormal.' href="javascript:;" rel= "1">1</a></li>';
		}
		for($i = $startpage; $i <= ($startpage + $numPageShow - 1) && $i <= $totalpage; $i++)
		{
			if($i == $currentpage)
			{
				$content.= '<li ><a '.$classCurrent.' href="javascript:;" rel="javascript:;">'.$i.'</a></li>';
			}
			else
			{
				$content.= '<li><a '.$classNormal.' href="javascript:;" rel= "'.$i.'">'.$i.'</a></li>';
			}
		}
		if($i == $totalpage)
		{
			$content.= '<li><a '.$classNormal.' href="javascript:;" rel= "'.$totalpage.'">'.$totalpage.'</a></li>';
		}
		else
			if($i < $totalpage and !$isNotAll)
			{
				$content.= '<li>...</li><li><a '.$classNormal.' href="javascript:;" rel= "'.$totalpage.'">'.$totalpage.'</a></li>';
			}
			//Trang sau
			if($currentpage < $totalpage)
			{
				$content.= '<li><A class="next '.$classText.'" href="javascript:;" rel="'.($currentpage+1).'" >Next</A></li>';
			}
			if($totalpage < 2)
				$content='';
			if($content == '')
				return $content;
			else
				return '<ul class="paging clearfix">'.$content.'</ul>';
	}
	/**
	 * Build url pagging
     * @author VuNN
	 *
	 * @param $total
	 * @param $perPage,
	 * @param $numPageShow
	 * @param $classNormal
	 * @param $page_name
	 * @param $classCurrent
	 * @param $pageCurrent
	 *
	 * @return string pagging
	 */
	function paginatorAdmin($total, $perPage, $numPageShow=10, $page_name='page',$pageCurrent = 0, $classCurrent = '')
	{
		$urlCurrent = $this->curPageURL($page_name);
		$content='';
		$totalpage = ceil($total/$perPage);
		$currentpage = $pageCurrent;
		if($currentpage == 'NULL')
			$currentpage = 1;
		// $currentpage=round($currentpage);
		if($classCurrent == '')
			$classCurrent = 'class="current"';
		else
			$classCurrent = 'class="'.$classCurrent.'"';

		if($currentpage <= 0 || $currentpage > $totalpage)
		{
			$currentpage = 1;
		}

		if($currentpage > ($numPageShow/2))
		{
			$startpage = $currentpage-floor($numPageShow/2);
			if(($totalpage - $startpage) < $numPageShow)
			{
				$startpage = $totalpage - $numPageShow + 1;
			}
		}
		else
		{
			$startpage = 1;
		}
		if($startpage < 1)
		{
			$startpage = 1;
		}
		//Link den trang truoc
		$url = '';
		if($currentpage > 1)
		{
			$url = $urlCurrent.$page_name.'='.($currentpage-1);
			$content.= '<span class="prev"><A  href="'.$url.' ">&lt; Previous</A></span>';
		}else{
			$content.= '<span class="prev disabled">&lt; Previous</span>';
		}
		//Danh sach cac trang
		if($startpage > 1)
		{
			$url = $urlCurrent.$page_name.'=1';
			$content.= '<span><a href="'.$url.' ">1</a></span>';
		}
		for($i = $startpage; $i <= ($startpage + $numPageShow - 1) && $i <= $totalpage; $i++)
		{
			if($i == $currentpage)
			{
				$content.= '<span '.$classCurrent.'>'.$i.'</span>';
			}
			else
			{
				$url = $urlCurrent.$page_name.'='.$i;
				$content.= '<span><a  href="'.$url.' ">'.$i.'</a></span>';
			}
		}
		if($i == $totalpage)
		{
			$url = $urlCurrent.$page_name.'='.$totalpage;
			$content.= '<span><a href="'.$url.' ">'.$totalpage.'</a></span>';
		}
		else
			if($i < $totalpage and !$isNotAll)
			{
				$url = $urlCurrent.$page_name.'='.$totalpage;
				$content.= '<span>...</span><span><a  href="'.$url.' ">'.$totalpage.'</a></span>';
			}
			//Trang Next
			if($currentpage < $totalpage)
			{
				$url = $urlCurrent.$page_name.'='.($currentpage+1);
				$content.= '<span class="next"><A class="next" href="'.$url.' ">Next &gt;</a></span>';
			}else{
				$content.= '<span class="next disabled">Next &gt;</span>';
			}
			if($totalpage < 2)
				$content='<span class="prev disabled">&lt; previous</span><span class="next disabled">next &gt;</span>';
			if($content == '')
				return $content;
			else
				return '<div class="paging">'.$content.'</div>';
	}
	/**
	 * get current URL
	 * 
	 * @author VuNN (11/10/2012)
     * 
     * @param $page_name
	 */
	function curPageURL($page_name){
		$pageURL = 'http';
		if ($_SERVER["HTTPS"] == "on") {
			$pageURL .= "s";
		}
		$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		if(strpos($pageURL,'?')){
			if(strpos($pageURL,'?'.$page_name) > 0){
				return substr($pageURL,0,strpos($pageURL,$page_name));
			}
			if(strpos($pageURL,$page_name) > 0){
				return substr($pageURL,0,strpos($pageURL,'&'.$page_name)).'&';
			}
			return $pageURL.'&';
		}
		else{
			return $pageURL.'?';
		}
	}
    /**
	 * get current URL
	 * 
	 * @author VuNN (11/10/2012)
     * 
     * @param $page_name
	 */
	function curPageURLNONE($page_name){
		$pageURL = 'http';
		if ($_SERVER["HTTPS"] == "on") {
			$pageURL .= "s";
		}
		$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}

	/**
	 * Paginator font-end
	 * 
	 * @author VuNN
     *
     * @param $total
     * @param $perPage
     * @param $numPageShow
     * @param $classNormal
     * @param $page_name
     * @param $classCurrent
     * @param $pageCurrent
	 */
	function buildNew($total, $perPage, $numPageShow=10, $classNormal='',$page_name='page_no', $classCurrent = '',$pageCurrent = 0)
	{
		$content='';
		$totalpage = ceil($total/$perPage);
		if($pageCurrent == 0)
			$currentpage = 1;
		else
			$currentpage = $pageCurrent;
		// $currentpage=round($currentpage);
		if($classCurrent == '')
			$classCurrent = 'class="on"';
		else
			$classCurrent = 'class="'.$classCurrent.'"';
		if($classNormal){
			$classText = $classNormal;
			$classNormal = 'class="'.$classNormal.'"';
		}
		if($currentpage <= 0 || $currentpage > $totalpage)
		{
			$currentpage = 1;
		}

		if($currentpage > ($numPageShow/2))
		{
			$startpage = $currentpage-floor($numPageShow/2);
			if(($totalpage - $startpage) < $numPageShow)
			{
				$startpage = $totalpage - $numPageShow + 1;
			}
		}
		else
		{
			$startpage = 1;
		}
		if($startpage < 1)
		{
			$startpage = 1;
		}
		//Link den trang truoc
		if($currentpage > 1)
		{
			$content.= '<A class="prev '.$classText.'" href="javascript:;" rel = "'.($currentpage-1).'" >Prev</A>';
		}
		//Danh sach cac trang
		if($startpage > 1)
		{
			$content.= '<a '.$classNormal.' href="javascript:;" rel= "1">1</a>';
		}
		for($i = $startpage; $i <= ($startpage + $numPageShow - 1) && $i <= $totalpage; $i++)
		{
			if($i == $currentpage)
			{
				$content.= '<a '.$classCurrent.' href="javascript:;" rel="javascript:;">'.$i.'</a>';
			}
			else
			{
				$content.= '<a '.$classNormal.' href="javascript:;" rel= "'.$i.'">'.$i.'</a>';
			}
		}
		if($i == $totalpage)
		{
			$content.= '<a '.$classNormal.' href="javascript:;" rel= "'.$totalpage.'">'.$totalpage.'</a>';
		}
		else
			if($i < $totalpage and !$isNotAll)
			{
				$content.= '...<a '.$classNormal.' href="javascript:;" rel= "'.$totalpage.'">'.$totalpage.'</a>';
			}
			//Trang Next
			if($currentpage < $totalpage)
			{
				$content.= '<A class="next '.$classText.'" href="javascript:;" rel="'.($currentpage+1).'" >Next</A>';
			}
			if($totalpage < 2)
				$content='';
			if($content == '')
				return $content;
			else
				return $content;
	}
}
?>